﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet.Ajax
{
	[Themeable(true)]
	[ParseChildren(false)]
	public class DivLayers : Swaf.Gui.AspNet2.DivLayers, IAjaxClientControl
	{
		protected string m_parentBizProcIds = null;
		protected BizProcWidgetType m_childVisibility = BizProcWidgetType.other;

		public DivLayers(): base(){}
		public DivLayers(ActiveWidget widget) { ID = widget.State.Id; }

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			ActiveWidget act = null;
			bool hasOfflineStateAlready = BPC.getWidgetOfflineState(WidgetId) != null;
			WidgetOfflineState offlineState = State;
			if (act == null)
			{
				act = new ActiveWidget(this, BPC, offlineState);
				act.CreatedFromPage = !hasOfflineStateAlready;
				BPC.defineActiveWidget(act);
			}

			if (State.State.get("m_childVisibility") == null)
				State.State.put("m_childVisibility", m_childVisibility);
		}

		public BizProcWidgetType ChildVisibility
		{
			get { return (BizProcWidgetType)getOffline("m_childVisibility"); }
			set { putOffline("m_childVisibility", value); }
		}

		protected override void Render(HtmlTextWriter writer)
		{
			string styleInfo = "";
			if (!IsVisble)
				styleInfo = "style='display:none;'";
			writer.Write(string.Format("<div id='{0}' {1}>", WidgetId, styleInfo));
			base.Render(writer);
			writer.Write("</div>");
		}

		#region IAjaxClientControl Members

		public bool CreateOnLoad
		{
			set {
				//Intentionally left blank as not needed by this class
			}
		}

		public void setState(System.Xml.XmlElement clientState)
		{
			//Intentionally left blank as not needed by this class
		}

		protected bool IsVisble
		{
			get
			{
				bool visible = true;
				if (ChildVisibility != BizProcWidgetType.other)
				{
					List<string> parentIds = State.ParentBizProcIds;
					foreach (string parentId in parentIds)
					{
						ActiveBizProc parent = BPC.getActiveBizProc(parentId);
						if (parent != null)
							visible = parent.BizProc.IsChildTypeVisible(ChildVisibility);
						if (!visible)
							break;
					}
				}
				return visible;			
			}
		}

		public void getState(System.Xml.XmlElement clientState, string pageHtml)
		{
			clientState.SetAttribute("independent", "true");
			clientState.SetAttribute("script", string.Format("$('#{0}').{1}();", WidgetId, IsVisble?"show":"hide"));
		}

		public void processAjaxEvent(System.Xml.XmlElement eventInfo)
		{
			//Intentionally left blank as not needed by this class
		}

		public bool NeedToGenerateHtmlWithServerControls
		{
			get { return false; }
		}

		#endregion

		#region IWidget Members

		public Swaf.Gui.BizProcWidgetType BizProcWidgetType
		{
			get
			{
				return BizProcWidgetType.other;
			}
			set
			{
				//Intentionally left blank as not needed by this class
			}
		}

		public string ParentBizProcIds
		{
			set { putOffline("m_parentBizProcIds", value); }
			get { return (string)getOffline("m_parentBizProcIds"); }
		}

		public string WidgetId
		{
			get { return ClientID; }
		}

		#endregion

		protected void putOffline(string keyName, object val)
		{
			if (BPC != null)
				State.State.put(keyName, val);
			Cmn.callPropertySet(this, keyName, val);
		}
		protected object getOffline(string keyName)
		{
			object v = BPC == null ? null : State.State.get(keyName);
			if (v == null)
				v = Cmn.callPropertyGet(this, keyName);
			return v;
		}

		protected BizProcCoordinator BPC
		{
			get { return (Page is IPage) ? ((IPage)Page).BPC : null; }
		}

		protected WidgetOfflineState State
		{
			get
			{
				WidgetOfflineState state = BPC==null?null:BPC.getWidgetOfflineState(WidgetId);
				if (state == null && BPC != null)
				{
					BPC.addWidgetOfflineState(state = new WidgetOfflineState(WidgetId, this.GetType().AssemblyQualifiedName));
					state.setParentBizProcIds(m_parentBizProcIds);
					state.BizProcWidgetType = this.BizProcWidgetType;
				}
				return state;
			}
		}
	}
}
